<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 矿机管理
 */
namespace app\product\model;

use app\product\model\Model;
// use  Mscore\Core\Lib\Upload;
use  Mscore\Core\Redis;

class Dueltrade extends Model{
	
    private $product = 'pro_dueltrade';

     /**
     * 初始化自动连接方法
     * @param array $__construct 
     * @param array $where 
    */
    public function __construct()  
    {    
        $this->hashfix=config('redis.redis_db.db_hashfix');
    }

    /**
     * 内容列表
     * @param array $list
     * @param array $where
     */ 
    public function list($where=null,$page=1,$page_size=10){
        //矿机列表
        $list['datalist']= Model::name($this->product)
            ->alias('a')
            ->where($where)
            ->limit(($page-1)*$page_size,$page_size)
            ->order('a.orderid desc')
            ->select();
        //数据总数
        $list['total']=Model::name($this->product)
            ->alias('a')
            ->where($where)
            ->count('id');
        return $list;
    }

    /**
     * 内容添加
     * @param array $info 
     * @param array $where 
     */ 
    public function info($id=null,$field='*'){  
        $info = Model::name($this->product)
           ->field($field)
           ->where(['id'=>$id])
           ->find();
        if(isset($info['titles'])&&!empty($info['titles'])){
            $info['titles']=json_decode($info['titles'],true);
            $i=0;
            foreach ($info['titles'] as $key => $value) {
                 $info['langinfo'][$key]['titles']=$value;
                 $info['langinfo'][$key]['id']=$i;
                 $i++;
             }  
        }
        if(isset($info['opentime'])){
            $info['opentime']=json_decode($info['opentime'],true);  
        }
        if(isset($info['beatpoint'])){
            $info['beatpoint']=json_decode($info['beatpoint'],true);  
        }
        return $info;
    }

    /**
     * 内容保存
     * @param array $productsave 
     * @param array $where 
     */ 
    public function save($data,$id=null){
        $id = !empty($data['id'])?$data['id']:false;
        if(isset($data['langs'])&&!empty($data['langs'])){
            foreach ($data['langs'] as $key => $value) {
                $titles[$value]=$data['titles'][$key];
            }
        };
        if($id){
            $info = $this->info($id,['imgurl']);
            $imgurl = !empty($data['imgOne']) ? $data['imgOne'] : $info['imgurl'];
        }else{
            $imgurl = $data['imgOne'];
        }
        $saveData = [
            'title'=>$data['title'],
            'cid'=>$data['cid'],
            'cname'=>$data['cname'],
            'procode'=>$data['procode'],
            'proncode'=>!empty($data['proncode'])?$data['proncode']:$data['procode'],
            'titles'=>json_encode($titles,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE),
            'orderid'=>$data['orderid'],
            'opentype'=>$data['opentype'],
            'opentime'=>json_encode($data['opentime'],JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE),
            'beatpoint'=>json_encode($data['beatpoint'],JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE),
            'status'=>$data['status'],
            'createtime'=>date("Y-m-d H:i:s", time()),
            'imgurl'=>$imgurl,
            'online'=>$data['online']
        ];
        if ($id) {
           Model::name($this->product)->where(' id=' . $id)->update($saveData);
           return true; 
        } else {
            Model::name($this->product)->insert($saveData);
            return true; 
        }
        return fasle;
    }

    /**
     * 内容删除
     * @param array $del 
     * @param array $where 
     */
    public function del($id=null){
     //删除
        if ($id) {
            Model::name($this->product)->where('id='.$id)->delete();
            return true;
        }
        return fasle;
    }

    /**
     * 操作redis
     * @param array $writeRedis
     * @param array $where $page $num
     */
    public function execRedis($meth,$arr,$db='0')
    {
        if($meth){
            $arr[0]=$this->hashfix.$arr[0];
            $data = Redis::exec($meth,$arr,$db);
            return $data;
        }        
        return false;
    }

}
